//
//  JsApiTest.m
//
//  Created by 杜文 on 16/12/30.
//  Copyright © 2016年 杜文. All rights reserved.
//

#import "JsApiTest.h"
#import "dsbridge.h"
#import "HWBookReadViewController.h"
#import "HWLoginViewController.h"
#import "HWIndexViewController.h"
#import "TempViewController.h"
#import "HWCommentCircleViewController.h"

@interface JsApiTest(){
  NSTimer * timer ;
  void(^hanlder)(id value,BOOL isComplete);
  int value;
}
@end

@implementation JsApiTest

- (NSString *)hideBar:(NSString *)msg
{
    return @"1";
}

- ( void )openBook:(id  _Nonnull)args
{
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:args];
    NSNumber *nub = IFNULL(dic[@"chapterCoin"], @0);
    NSNumber *chapterID = IFNULL(dic[@"readingChpaterId"], @0);
    NSNumber *chapterFreeNum = IFNULL(dic[@"chapterFreeNum"], @0);
//    HWBookReadViewController *vc = [[HWBookReadViewController alloc]init];
    
    if ([chapterID integerValue] !=0 || [nub integerValue] !=0  || [chapterFreeNum integerValue] !=0 )
    {
        TempViewController *vc = [[TempViewController alloc]init];
        vc.chapterCoin = [nub stringValue];
        vc.chapterId = [chapterID stringValue];
        vc.chapterFreeNum = [chapterFreeNum stringValue];
        vc.hidesBottomBarWhenPushed = YES;
        [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
    }
    else
    {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showErrorWithStatus:@"文章可能正在更新中，请稍后再试"];
        DISMISS_SVP(1.0f);
    }
}

- (void)unToken:(id  _Nonnull)args
{
    if ([args isEqualToString:@"0"])
    {
        HWLoginViewController *vc = [[HWLoginViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [[HWUtilties getCurrentVC].navigationController presentViewController:vc animated:YES completion:^{
        }];
    }
}

- ( void )openMenu:(id  _Nonnull)args
{
    NSDictionary *dic = [NSDictionary dictionaryWithDictionary:args];
    NSNumber *nub = IFNULL(dic[@"bookId"], [NSNumber new]);
    HWIndexViewController *vc = [[HWIndexViewController alloc]init];
    vc.booID = [nub stringValue];
    vc.hidesBottomBarWhenPushed = YES;
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}

//- (NSString *)fromBookShelf:(id  _Nonnull)args
//{
//    return @"1";
//}

- (void)openMComment:(id  _Nonnull)args
{
    HWCommentCircleViewController *vc = [[HWCommentCircleViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}


//- ( void )indexFinished:(id  _Nonnull)args
//{
//    
//}
//
//- ( void )bookDetailFinished:(id  _Nonnull)args
//{
//    
//}
//
//- ( void )haiEarnFinished:(id  _Nonnull)args
//{
//    
//}
//
//- ( void )haiMomentsFinished:(id  _Nonnull)args
//{
//    
//}

//- (void) hideBar:(NSString *) msg :(JSCallback) completionHandler
//{
//    completionHandler([msg stringByAppendingString:@"1"],YES);
//}

//- (NSString *)testNoArgSyn:(NSDictionary *) args
//{
//    return  @"testNoArgSyn called [ syn call]";
//}
//
//- ( void )testNoArgAsyn:(NSDictionary *) args :(JSCallback)completionHandler
//{
//    completionHandler(@"testNoArgAsyn called [ asyn call]",YES);
//}
//
//- ( void )callProgress:(NSDictionary *) args :(JSCallback)completionHandler
//{
//    value=10;
//    hanlder=completionHandler;
//    timer =  [NSTimer scheduledTimerWithTimeInterval:1.0
//                                              target:self
//                                            selector:@selector(onTimer:)
//                                            userInfo:nil
//                                             repeats:YES];
//}
//
//-(void)onTimer:t{
//    if(value!=-1){
//        hanlder([NSNumber numberWithInt:value--],NO);
//    }else{
//        hanlder(0,YES);
//        [timer invalidate];
//    }
//}

/**
 * Note: This method is for Fly.js
 * In browser, Ajax requests are sent by browser, but Fly can
 * redirect requests to native, more about Fly see  https://github.com/wendux/fly
 * @param requestInfo passed by fly.js, more detail reference https://wendux.github.io/dist/#/doc/flyio-en/native
 */
//-(void)onAjaxRequest:(NSDictionary *) requestInfo  :(JSCallback)completionHandler{
//
//}

@end
